package com.duanruan.bmgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.duanruan.bmgl.dao.DepartmentMapper;
import com.duanruan.bmgl.entity.Department;
import com.duanruan.bmgl.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 部门管理服务实现类
 *
 * @author makejava
 * @since 2020-08-21 12:10:23
 */
@Service("departmentServicebmgl")
public class DepartmentServiceImpl implements DepartmentService {
    @Resource(name = "DepartmentMapperbmgl")
    DepartmentMapper departmentMapper;

    //按部门编号、部门名称、类型查找部门
    @Override
    public List<Department> findDepartments(String id, String name, String type) {
        QueryWrapper<Department> wrapper=new QueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(id),"id",id)
                .like(!StringUtils.isEmpty(name),"name",name)
                .like(!StringUtils.isEmpty(type),"type",type);
        return departmentMapper.selectList(wrapper);
    }

    //添加新部门
    //-1代表id已存在，0代表插入失败，1代表插入成功
    @Override
    public int insertDepartment(Department department) {
        String id=department.getId();
        QueryWrapper<Department> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        int count=departmentMapper.selectCount(wrapper);
        System.out.println(count);
        if(count>0) return -1;
        return departmentMapper.insert(department);
    }

    //根据id删除部门
    //-1代表部门下有员工，0代表删除失败，1代表删除成功
    @Override
    public int deleteDepartment(int id) {
        int count=departmentMapper.selectEmployee(id);
        if(count==0){
            int num=departmentMapper.deleteById(id);
            if(num>0) return 1;
            else if(num==0) return 0;
        }
        return -1;
    }

    //修改部门信息
    @Override
    public int updateDepartment(Department department) {
        QueryWrapper<Department> wrapper=new QueryWrapper<>();
        wrapper.eq("id",department.getId());
        return departmentMapper.update(department,wrapper);
    }
}